package edu.odu.cs.sstore.client;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import edu.odu.cs.sstore.dto.DocumentDTO;
import edu.odu.cs.sstore.service.CourseService;
import edu.odu.cs.sstore.service.DocumentService;
import edu.odu.cs.sstore.service.UserService;

@Controller
public class DocumentController {
	
	@Autowired
	private CourseService courseService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private DocumentService docService;

	@RequestMapping(value="/document/add",method=RequestMethod.GET)
	public ModelAndView addDocument() {
		ModelAndView mav = new ModelAndView("document");
		mav.addObject("operation", "Add");
		mav.addObject("document",new DocumentDTO());
		mav.addObject("users",userService.getUsers());
		mav.addObject("courses",courseService.getCourses());
		return mav;
	}
	
	@RequestMapping(value="/document/add", method=RequestMethod.POST)
	public Object $addDocument(
			@ModelAttribute("document") @Valid DocumentDTO document,
			BindingResult result, 
			HttpServletRequest request, 
			HttpServletResponse response) 
	{
		
		// If there are errors, return to the view
		if(result.hasErrors()) {
			ModelAndView mav = new ModelAndView("document");
			mav.addObject("operation","Add");
			mav.addObject("document",document);
			mav.addObject("users",userService.getUsers());
			mav.addObject("courses",courseService.getCourses());
			return mav;
		}
		
		// Create the document
		this.docService.createDocument(document);
		
		// Redirect back to the faculty view
		return new RedirectView("/faculty",true);
	}
	
	
	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
		
		binder.registerCustomEditor(Set.class, "users", new CustomCollectionEditor(Set.class) {
		
			protected Object convertElement(Object element) {
				String id = null;

				if (element instanceof String) {
					id = (String) element;
				}

				return id != null ? userService.getUser(Long.valueOf(id)) : null;
			}
			
		});
		
		binder.registerCustomEditor(Set.class, "courses", new CustomCollectionEditor(Set.class) {
			
			protected Object convertElement(Object element) {
				String id = null;

				if (element instanceof String) {
					id = (String) element;
				}

				return id != null ? courseService.getCourse(Long.valueOf(id)) : null;
			}
			
		});
	}
}
